﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace 包覆门.ColorManager
{
    public partial class ColorList : Form
    {
        public ColorList()
        {
            InitializeComponent();
            this.dgColor.AutoGenerateColumns = false;
        }
        private ColorDAL dal = new ColorDAL();
        private Dictionary<int, string> dicGylb = new Dictionary<int, string>();//工艺类别
        private Dictionary<int, string> dicOld = new Dictionary<int, string>();//是否做旧


        //加载
        private void ColorList_Load(object sender, EventArgs e)
        {
            dicGylb.Add(0, "门型");
            dicGylb.Add(1,"装饰线条");

            dicOld.Add(0,"否");
            dicOld.Add(1, "是");

            BindingSource bsgylb = new BindingSource();
            bsgylb.DataSource = dicGylb;
            gylb.DataSource = bsgylb;
            gylb.ValueMember = "key";
            gylb.DisplayMember = "value";

            BindingSource bsOld = new BindingSource();
            bsOld.DataSource = dicOld;
            isold.DataSource=bsOld;
            isold.DisplayMember = "value";
            isold.ValueMember = "key";

            LoadData();//加载数据


        }
        //新增
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            ColorEditing ce = new ColorEditing();
            ce.IsNew = true;
            if (ce.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }   
        }
        //编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            int count = this.dgColor.SelectedRows.Count;
            if (count <= 0) {
                MessageBox.Show("请选择要编辑的行");
                return;
            }
            ColorEditing ce = new ColorEditing();
            ce.IsNew = false;
            ce.EditingId = this.dgColor.SelectedRows[0].Cells["mid"].Value.ToString();
            if (ce.ShowDialog() == DialogResult.OK) {
                LoadData();
            }   
        }
        //删除
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = this.dgColor.SelectedRows.Count;
            if (count <= 0) {
                MessageBox.Show("请选择要删除的行");
                return;
            }
            string info = this.dgColor.SelectedRows[0].Cells["mcolor"].Value.ToString();
            if(MessageBox.Show("确认要删除<"+info+">颜色么","提示信息",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)==DialogResult.OK){
                int result = dal.DelDataById(this.dgColor.SelectedRows[0].Cells["mid"].Value.ToString());
                if (result == 1)
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                }
                else {
                    MessageBox.Show("删除失败");
                }
            }
        }
        //关闭
        private void tsbClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        //加载数据
        private void LoadData() {
            string color = this.tbColor.Text.Trim();

            this.dgColor.DataSource = dal.ColorListAll(color);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
